package com.yatb.trac.util;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.Assert;

public class TracBeanUtils {

	public static Object copyRequestParametersToBean(
			HttpServletRequest request, Object theBean) {
		Assert.notNull(theBean,
				"Oggetto su cui effettuare la copia non puo' essere null!");
		Enumeration<String> parameterNames = request.getParameterNames();
		BeanWrapper bw = new BeanWrapperImpl(theBean);

		while (parameterNames.hasMoreElements()) {
			String paramName = (String) parameterNames.nextElement();
			String paramValue = request.getParameter(paramName);
			try {
				bw.setPropertyValue(paramName, paramValue);
			} catch (Exception e) {
				// NOTHING!!!
			}
		}
		return theBean;
	}
}
